package com.honey.solr.entity.method;

import java.util.List;

import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.calculator.JavaNameCalculator;
import com.honey.core.utils.JavaBeansUtil;
import com.honey.solr.SchemaField;
import com.honey.solr.SolrFactory;

public class ConstructorMethod {
	
	private SolrFactory solrFactory = null; 
	public ConstructorMethod( SolrFactory solrFactory ){
		this.solrFactory =  solrFactory;
	}
	
	public Method createMethod(){
		Method answer = new Method(solrFactory.getEntityType().getShortName(),true);
		List<SchemaField> list = solrFactory.getSchemaFields();
		JavaNameCalculator c = solrFactory.getJavaNameCalculator();
		boolean trimStrings = solrFactory.getSolrPlugin().isTargetEntityTrim();
		for(SchemaField sf : list ){
			String name = c.fieldNameCalculator(sf.getName());
			answer.addParameter(
				new Parameter(sf.getType(),name,sf.getComment())
			);
			if( trimStrings ){
				answer.addBodyLine(JavaBeansUtil.getSetterMethodName(name)+"( "+ name+" )" );
			}else{
				answer.addBodyLine("this." +name+" = "+name );
			}
		}	
		return answer;
	}
}
